package com.smm.service;

import com.jn.sqlhelper.dialect.pagination.PagingRequest;
import com.jn.sqlhelper.dialect.pagination.PagingRequestContextHolder;
import com.jn.sqlhelper.dialect.pagination.PagingResult;
import com.smm.dao.CommentDao;
import com.smm.entity.Comment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CommentService {
    @Resource
    private CommentDao commentDao;

    public Integer addComment(Comment comment) {
        LocalDateTime now = LocalDateTime.now();
        comment.setDate(LocalDateTime.now());
        return commentDao.addComment(comment);
    }

    public Integer deleteComment(Integer id) {
        return commentDao.deleteComment(id);
    }

    public Comment findComment(Comment comment) {
        return commentDao.findComment(comment);
    }

    public List<Comment> findCommentList(Comment comment) {
        return commentDao.findCommentList(comment);
    }

    public Integer updateComment(Comment comment) {
        comment.setDate(LocalDateTime.now());
        return commentDao.updateComment(comment);
    }

    public PagingResult findCommentPage(Comment comment, Integer pageSize, Integer pageNumber) {
        PagingRequest request = new PagingRequest().setPageNo(pageNumber).setPageSize(pageSize);
        PagingRequestContextHolder.getContext().setPagingRequest(request);
        List<Comment> comments = commentDao.findCommentList(comment);
        return request.getResult().setItems(comments);
    }

}
